package com.haowu.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.haowu.common.core.constant.UserConstants;
import com.haowu.common.core.utils.StringUtils;
import com.haowu.common.mybatis.core.page.PageQuery;
import com.haowu.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.haowu.customer.domain.bo.HwAppAccountBo;
import com.haowu.customer.domain.vo.HwAppAccountVo;
import com.haowu.customer.mapper.HwAppAccountMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.haowu.customer.api.domain.HwAppAccount;
import com.haowu.customer.service.IHwAppAccountService;

import java.util.List;
import java.util.Collection;

/**
 * 手机会员账户Service业务层处理
 *
 * @author lky
 * @date 2023-02-13
 */
@RequiredArgsConstructor
@Service
public class HwAppAccountServiceImpl implements IHwAppAccountService {

    private final HwAppAccountMapper baseMapper;

    /**
     * 查询手机会员账户
     */
    @Override
    public HwAppAccountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询手机会员账户列表
     */
    @Override
    public TableDataInfo<HwAppAccountVo> queryPageList(HwAppAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HwAppAccount> lqw = buildQueryWrapper(bo);
        Page<HwAppAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询手机会员账户列表
     */
    @Override
    public List<HwAppAccountVo> queryList(HwAppAccountBo bo) {
        LambdaQueryWrapper<HwAppAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HwAppAccount> buildQueryWrapper(HwAppAccountBo bo) {
        LambdaQueryWrapper<HwAppAccount> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, HwAppAccount::getUid, bo.getUid());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), HwAppAccount::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserPath()), HwAppAccount::getUserPath, bo.getUserPath());
        lqw.eq(StringUtils.isNotBlank(bo.getUserAccount()), HwAppAccount::getUserAccount, bo.getUserAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), HwAppAccount::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getUserPhone()), HwAppAccount::getUserPhone, bo.getUserPhone());
        lqw.eq(bo.getPlatform() != null, HwAppAccount::getPlatform, bo.getPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getInvoiceTitle()), HwAppAccount::getInvoiceTitle, bo.getInvoiceTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getGeTuiCid()), HwAppAccount::getGeTuiCid, bo.getGeTuiCid());
        lqw.eq(StringUtils.isNotBlank(bo.getRecommendCode()), HwAppAccount::getRecommendCode, bo.getRecommendCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HwAppAccount::getStatus, bo.getStatus());
        lqw.eq(bo.getIsDelete() != null, HwAppAccount::getIsDelete, bo.getIsDelete());
        return lqw;
    }

    /**
     * 新增手机会员账户
     */
    @Override
    public Boolean insertByBo(HwAppAccountBo bo) {
        HwAppAccount add = BeanUtil.toBean(bo, HwAppAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改手机会员账户
     */
    @Override
    public Boolean updateByBo(HwAppAccountBo bo) {
        HwAppAccount update = BeanUtil.toBean(bo, HwAppAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HwAppAccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除手机会员账户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 校验账号名是否重复
     * @param appAccount 账号
     * @return 结果
     */
    @Override
    public String checkAccountUnique(HwAppAccount appAccount) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<HwAppAccount>()
            .eq(HwAppAccount::getUserAccount, appAccount.getUserAccount())
            .ne(ObjectUtil.isNotNull(appAccount.getId()), HwAppAccount::getId, appAccount.getId()));
        if (exist) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * app会员注册
     */
    @Override
    public Boolean registerUser(HwAppAccount appAccount) {
        return baseMapper.insert(appAccount) > 0;
    }
}
